package edu.friday.controller;

import edu.friday.common.constant.Constants;
import edu.friday.common.result.RestResult;
import edu.friday.common.security.LoginUser;
import edu.friday.common.security.service.SysLoginService;
import edu.friday.common.security.service.SysPermissionService;
import edu.friday.common.security.service.TokenService;
import edu.friday.model.vo.SysMenuVO;
import edu.friday.model.vo.SysUserVO;
import edu.friday.service.SysMenuService;
import edu.friday.utils.http.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
public class LoginController {
    @Autowired
    SysLoginService sysLoginService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private SysMenuService menuService;

    @PostMapping({"/login","/"})
    public RestResult login(String username, String password,String uuid) {
        RestResult ajax=RestResult.success();
        String token= sysLoginService.login(username,password,"123",uuid);
        ajax.put(Constants.TOKEN,token);
        return ajax;
    }
    @GetMapping("/getInfo")
    public RestResult getInfo() {
        LoginUser loginUser=tokenService.getLoginUser(ServletUtils.getRequest());
        SysUserVO user=loginUser.getUser();
        //角色列表
        Set<String> roles=sysPermissionService.getRolePermission(user);
        //权限集合
        Set<String> permissions=sysPermissionService.getMenuPermission(user);
        RestResult ajax=RestResult.success();
        ajax.put("user",user);
        ajax.put("roles",roles);
        ajax.put("permissions",permissions);
        return ajax;
    }
    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public RestResult getRouters() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUserVO user = loginUser.getUser();
        List<SysMenuVO> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return RestResult.success(menuService.buildMenus(menus));
    }
}
